/*
 * Created on Feb 17, 2005
 * Created by dfhuynh
 */
package edu.mit.simile.longwell.ui.command;

import it.okkam.webid.model.VerifyWebID;

import java.util.Date;

import javax.servlet.ServletContext;

import org.apache.velocity.VelocityContext;

import edu.mit.simile.longwell.LongwellServer;
import edu.mit.simile.longwell.Profile;
import edu.mit.simile.longwell.ui.FlairMessage;
import edu.mit.simile.longwell.ui.FlairURL;
import edu.mit.simile.longwell.ui.InjectionManager;

/**
 * @author dfhuynh
 */
abstract public class CommandBase implements IFlairCommand {
	final protected InjectionManager m_injectionManager;
	protected ServletContext context;
	public CommandBase(InjectionManager injectionManager) {
		m_injectionManager = injectionManager;
	}

	protected InjectionManager createInjections(FlairMessage msg) {
		return (InjectionManager) m_injectionManager.clone();
	}

	protected VelocityContext createContext(FlairMessage msg) {
		VelocityContext vcContext = new VelocityContext();
		FlairURL		url = new FlairURL(msg);
		Profile			profile = msg.getProfile();
		Profile			profileFromSession = msg.getProfileFromSession();
		String[] dataset = (String[]) msg.m_request.getSession().getAttribute(msg.m_request.getSession().getId());
		vcContext.put("longwell", 		LongwellServer.getServer());
		vcContext.put("profile", 		profile);
		//vcContext.put("schemaModel", 	profile.getSchemaModel());
		vcContext.put("schemaModel", 	profileFromSession.getSchemaModel());
		vcContext.put("urlRedirect", 	url.dup().toURLString());
		vcContext.put("msg", 			msg);
		vcContext.put("url", 			url);
		vcContext.put("outerURL", 		url);
		vcContext.put("contextPath", 	url.getContextPath());
		vcContext.put("resourcePath", 	url.getContextPath() + "/resources");
		
		//property labels
		vcContext.put("locale", 		"");
		
		//interface language
		vcContext.put("languageBundle", profile.getLanguageBundle());
		vcContext.put("currentTime",	new Date());
		vcContext.put("utilities",		new Utilities());

		
		vcContext.put("serverlUrl", LongwellServer.getServerURL());
		vcContext.put("httpsPort", LongwellServer.getHttpsPort());
		
		vcContext.put("injections", 	createInjections(msg));
		vcContext.put("version",	LongwellServer.getVersion());
		vcContext.put("release",	LongwellServer.getRelease());
		vcContext.put("response", 		msg.m_response);
		vcContext.put("username", msg.getProfile().getUserName());
		vcContext.put("sessionId", msg.m_request.getSession().getId());
		vcContext.put("applicationName", LongwellServer.getApplicationName());
		vcContext.put("maxValueForFacet", LongwellServer.getMaxValueForFacet());
		vcContext.put("coordinateOnt", LongwellServer.getCoordinateOnt());
		vcContext.put("useMap",LongwellServer.getUseMap());
		context = msg.m_servlet.getServletContext();
		String fileName = LongwellServer.getUsersDatasetPath(); 
		boolean isAdmin = VerifyWebID.isAdmin(msg.getProfile().getUserName(), fileName);
		if(isAdmin){
			vcContext.put("isAdmin", "true");
		}
		else{
			vcContext.put("isAdmin", "false");
		}
		
		if(dataset!=null)
			vcContext.put("datasetSelected",dataset[0]);
		else
			vcContext.put("datasetSelected","notYet");
		return vcContext;
	}
}
